<!DOCTYPE html>
<html>
<head>
	<title></title>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<link rel="stylesheet" href="../css/reset.css">
	<link rel="stylesheet" href="multStatics/demo.css" type="text/css">
	<link rel="stylesheet" href="multStatics/zTreeStyle.css" type="text/css">
	<script type="text/javascript" src="multStatics/jquery-1.4.4.min.js"></script>
	<script type="text/javascript" src="multStatics/jquery.ztree.core-3.5.js"></script>
	<script type="text/javascript" src="multStatics/jquery.ztree.excheck-3.5.js"></script>
	<script src="../js/server.js"></script>
	<script type="text/javascript">
        var p = [];
        var zNodes = [];
        var setting = {
            check: {
                enable: true,
                chkboxType: {"Y":"", "N":""}
            },
            view: {
                dblClickExpand: false
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            callback: {
                beforeClick: beforeClick,
                onCheck: onCheck
            }
        };
        //ajax请求
        $.ajax({
            type: 'get',
            url: window.parent.baseUrl + 'publicInfo/ajlx',
            data:'',
            beforeSend: function(){
            },
            success: function(response){
                if(response.responseCode == '1000'){
                    zNodes = response.content;
                    $.fn.zTree.init($("#treeDemo"), setting, zNodes);
                } else {
                    $.fn.zTree.init($("#treeDemo"), setting, zNodes);
                }
            },
			error: function(){
			}
        })

        function beforeClick(treeId, treeNode) {
            var zTree = $.fn.zTree.getZTreeObj("treeDemo");
            zTree.checkNode(treeNode, !treeNode.checked, null, true);
            return false;
        }

        function onCheck(e, treeId, treeNode) {
            p = [];
            var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
                nodes = zTree.getCheckedNodes(true);
            for (var i=0, l=nodes.length; i<l; i++) {
                p.push("'"+ nodes[i].code + "'");
            }
        }

        function showMenu() {
            var cityObj = $("#citySel");
            var cityOffset = $("#citySel").offset();
            $("#menuContent").css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast");
            $("body").bind("mousedown", onBodyDown);
        }
        function hideMenu() {
            $("#menuContent").fadeOut("fast");
            $("body").unbind("mousedown", onBodyDown);
        }
        function onBodyDown(event) {
            if (!(event.target.id == "menuBtn" || event.target.id == "citySel" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
                hideMenu();
            }
        }

        $(document).ready(function(){
            $.fn.zTree.init($("#treeDemo"), setting, zNodes);
        });
        function clearCheck(){
            p = [];
            var zTree = $.fn.zTree.getZTreeObj("treeDemo");
            zTree.checkAllNodes(false);
            var nodes = zTree.getCheckedNodes(true);
            for (var i=0, l=nodes.length; i<l; i++) {
                p.push("'"+ nodes[i].code + "'");
            }
            $('.searchInput').val('');
            InitialZtree();
        }
        function InitialZtree(){
            $.fn.zTree.init($("#treeDemo"), setting, zNodes);
		}
        ///根据文本框的关键词输入情况自动匹配树内节点 进行模糊查找
        function AutoMatch(txtObj) {
            if (txtObj.value.length > 0) {
                InitialZtree();
                var zTree = $.fn.zTree.getZTreeObj("treeDemo");
                var nodeList = zTree.getNodesByParamFuzzy("name", txtObj.value);
                //将找到的nodelist节点更新至Ztree内
                $.fn.zTree.init($("#treeDemo"), setting, nodeList);
            } else {
                InitialZtree();
            }
        }
	</script>
</head>

<body>
<div id="menuContent" class="menuContent" style="display:block; position: absolute;overflow: hidden;">
	<button type="button" class="btn btn-success" onclick="clearCheck()">清空</button>
	<button>查找</button><input type="text"  class="searchInput" type="text" value="" onkeyup="AutoMatch(this)">
	<ul id="treeDemo" class="ztree" >
	</ul>
</div>
</body>
</html>